package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Role;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IPermissionService;
import cn.wolfcode.crm.service.IRoleService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private IRoleService roleService;
    @Autowired
    private IPermissionService permissionService;

    @RequestMapping("")
    public String index() {
        return "role";
    }

    @RequestMapping("list")
    @ResponseBody
    public PageResult query(QueryObject qo) {
        return roleService.query(qo);
    }

    @RequestMapping("listAll")
    @ResponseBody
    public List<Role> listAll() {
        return roleService.listAll();
    }


    @RequestMapping("save")
    @ResponseBody
    public JsonResult save(Role role) {
        JsonResult jsonResult = new JsonResult();
        try {
            roleService.save(role);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setMsg("新增失败");
        }

        return jsonResult;
    }

    @RequestMapping("update")
    @ResponseBody
    public JsonResult update(Role role) {
        JsonResult jsonResult = new JsonResult();
        try {
            roleService.update(role);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setMsg("保存失败");
        }
        return jsonResult;
    }

    @RequestMapping("delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        JsonResult jsonResult = new JsonResult();
        try {
            roleService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setMsg("保存失败");
        }
        return jsonResult;
    }
    @RequestMapping("insertPermissionsId")
    @ResponseBody
    public JsonResult insertPermissionsId(Role role) {
        JsonResult jsonResult = new JsonResult();
        try {
            permissionService.insertPermissionsId(role);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setMsg("保存权限失败");
        }
        return jsonResult;
    }
    @RequestMapping("listIdsByOperatorId")
    @ResponseBody
    public List<Long> listIdsByOperatorId(Long operatorId) {

        return permissionService.listIdsByOperatorId(operatorId);

    }

}
